const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
/**
 * 关闭当前页面，返回上一页面或多级页面,并可携带参数
 * @param {*} data 
 * {
    delta:1, //需要返回几个页面 默认1
    data:{
      key:value
    },
    back:true //返回 false 不返回
  }
  在返回页面通过 this.data.key 获取返回值

  注意：返回数据使用完后记得清空以免引发问题。
 */
const navigateBackResult = data => {
  //获取当前页面栈
  let currentPage = getCurrentPages()
  //返回几个页面栈
  let delta = data.delta
  if (!delta) {
    delta = 1
  }
  //获取返回页面栈
  let prePage = currentPage[currentPage.length - (delta + 1)]
  if (data.data) {
    prePage.setData(data.data)
  }
  let back = data.back
  console.log('back start',back)
  if (back == undefined || back == null) {
    back = true
  }
  console.log('back end',back)
  if (back) {
    wx.navigateBack({
      delta: delta
    })
  }


}
module.exports = {
  formatTime,
  navigateBackResult
}